package ooo.reindeer.data.util;

public class DataUtil {
    public static boolean compare(Byte a, long b) {
        long c = a & 0xff;
        return c == b;
    }

    public static boolean compare(Short a, long b) {
        long c = a & 0xffff;
        return c == b;
    }

    public static boolean compare(Integer a, long b) {
        //noinspection PointlessBitwiseExpression
        long c = a & 0xffffffff;
        return c == b;
    }

    public static boolean compare(Long a, long b) {

        return a == b;
    }

    public static boolean compare(Object a, long b) {
        if (a instanceof Byte) {
            return compare((Byte) a, b);
        } else if (a instanceof Short) {
            return compare((Short) a, b);
        } else if (a instanceof Integer) {
            return compare((Integer) a, b);
        } else if (a instanceof Long) {
            return compare((Long) a, b);
        }

        return false;
    }

    public static Object convert(@SuppressWarnings("rawtypes") Class clazz, Object value) {
        Object ret;
        if (String.class == clazz) {
            ret = value.toString();
        } else if (Byte.class == clazz || "byte".equals(clazz.getName())) {
            ret = Byte.parseByte(value.toString());
        } else if (Short.class == clazz || "short".equals(clazz.getName())) {
            ret = Short.parseShort(value.toString());
        } else if (Integer.class == clazz || "int".equals(clazz.getName())) {
            ret = Integer.parseInt(value.toString());
        } else if (Long.class == clazz || "long".equals(clazz.getName())) {
            ret = Long.parseLong(value.toString());
        } else {
            ret = value;
        }

        return ret;
    }

    public static Object defaultValue(@SuppressWarnings("rawtypes") Class clazz) {
        try {
            if ("[B".equals(clazz.getName())) {
                return new byte[0];
            } else {
                return clazz.newInstance();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static int negate(int value) {
        return (value * -1);
    }
}
